<?php
if( !defined('CORE') ) exit('Request Error!');

class tpl
{
    protected static $instance = null;
    
    /**
     * Smarty 初始化
     * @return resource
     */
    public static function init ()
    {
        if (self::$instance === null)
        {
            require_once PATH_LIBRARY . '/smarty/Smarty.class.php';
            self::$instance = new Smarty();
            self::$instance->template_dir = util::path_exists(PATH_ROOT . '/'.APP_NAME.'/templates/template/');
            self::$instance->compile_dir = util::path_exists(PATH_ROOT . '/'.APP_NAME.'/templates/compile/');
            self::$instance->cache_dir = util::path_exists(PATH_ROOT . '/'.APP_NAME.'/templates/cache/');
            self::$instance->left_delimiter = '<{';
            self::$instance->right_delimiter = '}>';
            self::$instance->caching = false;
            self::$instance->compile_check = true;
            self::$instance->plugins_dir[] = util::path_exists(PATH_LIBRARY . '/smarty_plugins');
            self::config();
        }
        return self::$instance;
    }
    
    protected static function config ()
    {
        $instance = self::init();
        $instance->assign('URL', config('URL'));
        $instance->assign('request', req::$forms);
    }
    
    public static function assign ($tpl_var, $value)
    {
        $instance = self::init();
        $instance->assign($tpl_var, $value);
    }
    
    public static function display ($tpl)
    {
        $instance = self::init();
        $instance->display($tpl);
    }

    public static function fetch($tpl)
    {
        $instance = self::init();
        return $instance->fetch($tpl);
    }
}

